<template>
    <Form ref="formValidate" :model="formValidate" :label-width="80" :rules="ruleValidate" style="position: relative;">

      <FormItem label="入学年份" prop="start_year"  style="width:50%">
        <Col span="12">
        <DatePicker transfer v-model="formValidate.start_year" type="month"  placeholder="请选择时间" style="width: 200px"></DatePicker>
        </Col>
      </FormItem>
      <FormItem label="入学志愿" prop="volunteer" style="width:50%" >
        <Select v-model="formValidate.volunteer"  style="width:80%" >
          <Option v-for="item in listDictionary.enrollment" :value="item.dictionary_id" :key="item.dictionary_id"> {{item.dictionary_name.zh?item.dictionary_name.zh:item.dictionary_name.mn}}</Option>
        </Select>
      </FormItem>


        <FormItem  style="width:50%" label="蒙语文">
            <InputNumber style="width:100%" :min="0" v-model="formValidate.mongolian_chinese"></InputNumber>
        </FormItem>
        <FormItem  style="width:50%" label="数学">
            <InputNumber style="width:100%" :min="0" v-model="formValidate.mathematics"></InputNumber>
        </FormItem>
        <FormItem  style="width:50%" label="汉语">
            <InputNumber style="width:100%" :min="0" v-model="formValidate.chinese"></InputNumber>
        </FormItem>
        <FormItem  style="width:50%" label="蒙授英文">
            <InputNumber style="width:100%" :min="0" v-model="formValidate.mongolian_english"></InputNumber>
        </FormItem>
        <FormItem  style="width:50%" label="思想品德">
            <InputNumber style="width:100%" :min="0" v-model="formValidate.moral"></InputNumber>
        </FormItem>
        <FormItem  style="width:50%" label="历史">
            <InputNumber style="width:100%" :min="0" v-model="formValidate.history"></InputNumber>
        </FormItem>
        <FormItem  style="width:50%" label="物理">
            <InputNumber style="width:100%" :min="0" v-model="formValidate.physics"></InputNumber>
        </FormItem>
        <FormItem  style="width:50%" label="化学">
            <InputNumber style="width:100%" :min="0" v-model="formValidate.chemistry"></InputNumber>
        </FormItem>
        <FormItem  style="width:50%" label="信息技术">
            <InputNumber style="width:100%" :min="0" v-model="formValidate.information_echnology"></InputNumber>
        </FormItem>
        <FormItem  style="width:50%" label="体育">
            <InputNumber style="width:100%" :min="0" v-model="formValidate.sports"></InputNumber>
        </FormItem>
        <FormItem  style="width:50%" label="生物">
            <InputNumber style="width:100%" :min="0" v-model="formValidate.biological"></InputNumber>
        </FormItem>
        <FormItem  style="width:50%" label="地理">
            <InputNumber style="width:100%" :min="0" v-model="formValidate.geography"></InputNumber>
        </FormItem>
        <FormItem  style="width:50%" label="理化实验">
            <InputNumber style="width:100%" :min="0" v-model="formValidate.experiment"></InputNumber>
        </FormItem>
        <FormItem  style="width:50%" label="照顾分">
            <InputNumber style="width:100%" :min="0" v-model="formValidate.take_care_score"></InputNumber>
        </FormItem>
        <FormItem  style="width:50%" label="特长得分">
            <InputNumber style="width:100%" :min="0" v-model="formValidate.specialty_score"></InputNumber>
        </FormItem>
        <FormItem  style="width:50%" label="总分（含照顾分">
            <InputNumber style="width:100%" :min="0" v-model="formValidate.total_score"></InputNumber>
        </FormItem>



        <div style="width:90%;">
            <FormItem style="width:100%;">
            <Button type="primary" @click="handleSubmit('formValidate')">保存</Button>
            </FormItem>
        </div>
        <div style="clear:both"></div>
        <Spin size="large" fix v-if="spinShow"></Spin>
    </Form>
</template>

<script>

  export default {
    props: {
        user_id: {
            type: [Number],
            default: false
        },
        infoId: {
            type: [Boolean,String,Number],
            default: false
        }
    },
    data () {
        return {
            infoUrl:`api/student/v1/juniorcourse/lists/${this.infoId}`,//请求单条数据
            saveUrl:'',//保存接口
            saveType:null,//保存方式

            spinShow:false,//加载动画

            infoOld:null,
            listDictionary:{
                enrollment:[]
            }, 

            formValidate: {
                start_year:null,//入学年份
                mongolian_chinese:0,//蒙语文
                mathematics:0,    //数学 、
                chinese:0,    //汉语 、
                mongolian_english:0,     //蒙授英文 
                moral:0, //思想品德
                history:0,        //历史
                physics:0,       //物理
                chemistry:0,       //化学   
                information_echnology:0,       //信息技术  
                sports:0,       //体育   
                biological:0,       //生物   
                geography:0,       //地理   
                experiment:0,       //理化实验   
                take_care_score:0,       //照顾分   
                specialty_score:0,       //特长得分   
                total_score:0,       //总分（含照顾分）   
                volunteer:null,       //志愿 
                user_id:this.user_id  
            },
            ruleValidate: {
                // 志愿 
                volunteer: [
                    { type:'number',required: true, message: '请选择志愿', trigger: 'change' }
                ],
                // 入学年份
                start_year: [
                    { required: true, type: 'date', message: '请选择入学年份', trigger: 'blur' }
                ],
            },  
        }
    },
    methods: {
        /************************************************************
         * 
         * 单条数据请求
         * 
         ************************************************************/
        infoData(){
            this.spinShow=true//输入框禁用  
            this.$http.get(this.infoUrl).then(val=>{ 
                this.infoOld = JSON.parse(JSON.stringify(val.data))
                this.formValidate.mongolian_chinese = parseInt(this.infoOld.mongolian_chinese)//用于默认选的 蒙语文 
                this.formValidate.mathematics = parseInt(this.infoOld.mathematics)//用于默认选的 数学
                this.formValidate.chinese = parseInt(this.infoOld.chinese)//用于默认选的 汉语
                this.formValidate.mongolian_english = parseInt(this.infoOld.mongolian_english)//用于默认选的 蒙授英文
                this.formValidate.moral = parseInt(this.infoOld.moral)//用于默认选的 思想品德
                this.formValidate.history = parseInt(this.infoOld.history)//用于默认选的 历史
                this.formValidate.physics = parseInt(this.infoOld.physics)//用于默认选的 物理
                this.formValidate.chemistry = parseInt(this.infoOld.chemistry)//用于默认选的 化学 
                this.formValidate.information_echnology = parseInt(this.infoOld.information_echnology)//用于默认选的 信息技术  
                this.formValidate.sports = parseInt(this.infoOld.sports)//用于默认选的 体育  
                this.formValidate.biological = parseInt(this.infoOld.biological)//用于默认选的 生物 
                this.formValidate.geography = parseInt(this.infoOld.geography)//用于默认选的地理 
                this.formValidate.experiment = parseInt(this.infoOld.experiment)//用于默认选的 理化实验
                this.formValidate.take_care_score = parseInt(this.infoOld.take_care_score)//用于默认选的 照顾分 
                this.formValidate.specialty_score = parseInt(this.infoOld.specialty_score)//用于默认选的 特长得分
                this.formValidate.total_score = parseInt(this.infoOld.total_score)//用于默认选的 总分（含照顾分
                this.formValidate.volunteer = parseInt(this.infoOld.volunteer)//用于默认选的 志愿  


                this.formValidate.start_year = this.infoOld.start_year// 入学年份 
                this.formValidate.user_id=this.infoOld.user_id;//用户id
                this.spinShow=false//输入框禁用  

         
            })
            .catch(err => {
                console.log(err);
                if(err.code && err.code==422) {
                    this.$Message.error(err.message);
                }
                this.spinShow=false//输入框禁用  
            })
        },
        
        /************************************************************
         * 
         * 提交时
         * 
         ************************************************************/
        handleSubmit (name) {
            this.$refs[name].validate((valid) => {
                if (valid) {       
                    this.spinShow=true//输入框禁用   
                    this.saveType(this.saveUrl,this.formValidate).then(val=>{
                        this.$emit('expandback',true)
                        this.$Message.success(val.message);
                        this.spinShow=false
                    }).catch(err => {
                        if (err.code && err.code == 422) {
                            this.$Message.error(err.message);
                        }
                        this.spinShow=false
                    })  
                } else {
                    this.$Message.error('表单验证失败!');
                }
            })
        },
        

        /************************************************************
         * 
         * 查询资格类型
         * 
         ************************************************************/
        listDictionaryFun(){
            this.spinShow=true
            this.$http.get('api/dictionary/v1/dictionary/pidquerys?enrollment')
            .then(val=>{
                this.listDictionary = val.data
                this.spinShow=false
            })
            .catch(err => {
                console.log(err);
                if(err.code && err.code==422) {
                    this.$Message.error(err.message);
                }
                this.spinShow=false
            })
        }
    },
    created() {
        this.listDictionaryFun()
        if(this.infoId==false){
            // add
            this.saveUrl='api/student/v1/juniorcourse/create/'
            this.saveType=this.$http.post

        }else{
            // edit
            this.infoData();
            this.saveUrl=`api/student/v1/juniorcourse/update/${this.infoId}`
            this.saveType=this.$http.put
        }
    },
  }
</script>
<style lang="less" scoped>
  .ivu-form-item{
    width:50%;
    float:left;
  }
</style>
